package com.ruoyi.web.controller.teacher;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.domain.BasisUav;
import com.ruoyi.domain.UserData;
import com.ruoyi.dto.BasisLoadingPositionEquipmentUser;
import com.ruoyi.dto.BasisPositionFrequencyConverterUser;
import com.ruoyi.dto.BasisPositionReceiverUser;
import com.ruoyi.dto.BasisPositionResourceInfoUser;
import com.ruoyi.service.IUserDataService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userData")
public class UserDataController {

    @Autowired
    IUserDataService userDataService;

    @Autowired
    ISysUserService userService;

    @PostMapping("/addBasisPositonUser")
    public Object  addBasisPositonUser (@RequestBody UserData userData){
        boolean flag = false;
        String[] split = userData.getUserIds().split(",");
        for (String userid : split) {
            userData.setUserId(Integer.valueOf(userid));
            flag = userDataService.addBasisPositionUser(userData);
            if (!flag) return flag;
        }
        return flag;

    }

    //添加 案例 阵地 设备  人员  关联表
    @PostMapping("/addBasisLoadingPositionEquipmentUser")
    public Object  addBasisLoadingPositionEquipmentUser (@RequestBody BasisLoadingPositionEquipmentUser basisLoadingPositionEquipmentUser){
        boolean flag = false;

        String[] split = basisLoadingPositionEquipmentUser.getEquipmentIds().split(",");
        for (String eqid : split) {
            basisLoadingPositionEquipmentUser.setEquipmentId(Integer.valueOf(eqid));
            flag =userDataService.addBasisLoadingPositionEquipmentUser(basisLoadingPositionEquipmentUser);
            if (!flag) return flag;
        }

        return flag;
    }

    //新 添加 案例 阵地 设备  人员  关联表
    @PostMapping("/addBasisLoadingPositionResourcesUser")
    public Object  addBasisLoadingPositionResourcesUser (@RequestBody BasisPositionResourceInfoUser basisPositionResourceInfoUser){
        boolean flag = false;

        String[] split = basisPositionResourceInfoUser.getResourceIds().split(",");
        for (String rid : split) {
            basisPositionResourceInfoUser.setResourceId(Integer.valueOf(rid));
            flag =userDataService.addBasisLoadingPositionResourcesUser(basisPositionResourceInfoUser);
            if (!flag) return flag;
        }

        return flag;
    }



    //添加 案例 阵地 变频器  人员  关联表
    @PostMapping("/addBasisPositionFrequencyConverterUser")
    public Object  addBasisPositionFrequencyConverterUser (@RequestBody BasisPositionFrequencyConverterUser basisPositionFrequencyConverterUser){
        boolean flag = false;

        String[] split = basisPositionFrequencyConverterUser.getFrequencyConverterIds().split(",");
        for (String converterId : split) {
            basisPositionFrequencyConverterUser.setFrequencyConverterId(Integer.valueOf(converterId));
            flag =userDataService.addBasisPositionFrequencyConverterUser(basisPositionFrequencyConverterUser);
            if (!flag) return flag;
        }

        return flag;
    }


    //添加 案例 阵地 收发设备  人员  关联表
    @PostMapping("/addBasisPositionReceiverUser")
    public Object  addBasisPositionReceiverUser (@RequestBody BasisPositionReceiverUser basisPositionReceiverUser){
        boolean flag = false;

        String[] split = basisPositionReceiverUser.getReceiverIds().split(",");
        for (String receiverId : split) {
            basisPositionReceiverUser.setReceiverId(Integer.valueOf(receiverId));
            flag =userDataService.addBasisPositionReceiverUser(basisPositionReceiverUser);
            if (!flag) return flag;
        }

        return flag;
    }


    @GetMapping("/findByPositionIdFormUser")
    public AjaxResult findByPositionId(Long positionId, String loadingId){

        return AjaxResult.success(userDataService.findByPositionId(positionId, loadingId));
    }

    @GetMapping("/findByLoadingIdFormUser")
    public AjaxResult findByLoadingIdFormUser(String loadingId) {
        return AjaxResult.success(userDataService.findByLoadingIdFormUser(loadingId));
    }

    //新 查询人员下的设备
    @GetMapping("/findByLoadingIdFormUserResource")
    public AjaxResult findByLoadingIdFormUserResource(String loadingId) {
        return AjaxResult.success(userDataService.findByLoadingIdFormUserResource(loadingId));
    }
    @GetMapping("/findByLoadingIdAndUserIdFormUserResource")
    public AjaxResult findByLoadingIdAndUserIdFormUserResource(String loadingId,Long userId) {
        return AjaxResult.success(userDataService.findByLoadingIdAndUserIdFormUserResource(loadingId,userId));
    }


    //查询设备下的人员
    @GetMapping("/findByBasisPositionEquipmentUser")
    public AjaxResult findByBasisPositionEquipmentUser(String loadingId,Long positionId,Long equipmentId){

        return AjaxResult.success(userDataService.findByBasisPositionEquipmentUser(loadingId, positionId, equipmentId));
    }

    //根据案例阵地人员id查询  设备关联
    @GetMapping("/findByBasisPositionUserEq")
    public AjaxResult findByBasisPositionUserEq(String loadingId, Long positionId){

        return AjaxResult.success(userDataService.findByBasisPositionUserEq(loadingId, positionId));
    }

    //新 根据案例阵地人员id查询  设备关联
    @GetMapping("/findByBasisPositionUserResource")
    public AjaxResult findByBasisPositionUserResource(String loadingId, Long positionId){

        return AjaxResult.success(userDataService.findByBasisPositionUserResource(loadingId, positionId));
    }

//    //根据案例阵地人员id查询  变频器关联
//    @GetMapping("/findByBasisPositionUserEq")
//    public AjaxResult findByBasisPositionUserEq(String loadingId, Long positionId){
//
//        return AjaxResult.success(userDataService.findByBasisPositionUserEq(loadingId, positionId));
//    }
//    //根据案例阵地人员id查询  收发设备关联
//    @GetMapping("/findByBasisPositionUserEq")
//    public AjaxResult findByBasisPositionUserEq(String loadingId, Long positionId){
//
//        return AjaxResult.success(userDataService.findByBasisPositionUserEq(loadingId, positionId));
//    }


    //根据案例id 阵地id  人员id  删除阵地下对应的人员
    @GetMapping("/delBasisPositionUser")
    public AjaxResult delBasisPositionUser(String loadingId, Integer positionId, Integer userId){

        boolean flag = false;
        flag = userDataService.delBasisPositionUser(loadingId, positionId, userId);
        if (flag){
            flag = userDataService.delBasispositionEquipmentUser(loadingId, positionId, userId);
            if (!flag) return AjaxResult.success(flag);
        }

        return AjaxResult.success(flag);
    }


    //只删除人员
    @GetMapping("/delBasisPositionUser2")
    public AjaxResult delBasisPositionUser2(String loadingId, Integer positionId){

        boolean flag = false;


            flag = userDataService.delBasisPositionUser2(loadingId, positionId);


        return AjaxResult.success(flag);
    }

    //TODO
    //根据案例id 阵地id  人员id  删除人员绑定的设备
    @GetMapping("/delBasispositionEquipmentUser")
    public AjaxResult delBasispositionEquipmentUser(String loadingId, Integer positionId, Integer userId){

        return AjaxResult.success(userDataService.delBasispositionEquipmentUser(loadingId, positionId,userId));
    }


    @PostMapping("/addBasisPositonUserNew")
    public Object  addBasisPositonUserNew (@RequestBody UserData userData){
        boolean flag = false;
        String[] split = userData.getUserIds().split(",");
        for (String userid : split) {
            if (userid.equals(userData.getUserRole())){
                userData.setRoleId(1);
            }else{
                userData.setRoleId(0);
            }
            userData.setUserId(Integer.valueOf(userid));

            flag = userDataService.addBasisPositionUser(userData);
            if (!flag) return flag;
        }
        return flag;

    }
}
